package com.yunpeng.aidevice.common.controllers.configs;

import com.alibaba.fastjson.JSONObject;
import com.yunpeng.aidevice.biz.service.IPermissionBizService;
import com.yunpeng.aidevice.common.constant.ConfigConstantValues;
import com.yunpeng.common.dto.Result;
import com.yunpeng.common.paging.PagingQueryDTO;
import com.yunpeng.common.util.ParametersCheckUtil;
import com.yunpeng.aidevice.permission.service.IPermissionOuterService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;

/**
 * OrganizationPermissionController
 *
 * @author : Moore(hemw)
 * @date : 2020-04-15
 */
@RestController
@RequestMapping("/api/organizationPermission")
public class OrganizationPermissionController {

    @Resource
    private IPermissionBizService permissionBizService;

    @Resource
    private IPermissionOuterService permissionOuterService;

    /**
     * 组织机构权限分页列表
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "list", method = RequestMethod.POST)
    public Result<PagingQueryDTO> list(@RequestBody JSONObject requestParam) {
        ParametersCheckUtil.mapIsNotNullOrderEmpty(requestParam, "没有传递任何参数");
        String organizationName = requestParam.getString("organizationName");
        String permissionName = requestParam.getString("permissionName");
        Integer pageSize = requestParam.getInteger("pageSize");
        Integer currentPage = requestParam.getInteger("currentPage");
        return permissionBizService.queryOrgPermissionList(organizationName, permissionName, pageSize, currentPage);
    }

    /**
     * 绑定权限到组织机构
     *
     * @param requestParam
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "bind", method = RequestMethod.POST)
    public Result<Void> bind(@RequestBody JSONObject requestParam) {
        ParametersCheckUtil.mapIsNotNullOrderEmpty(requestParam, "没有传递任何参数");
        String organizationCode = requestParam.getString("organizationCode");
        String permissionCode = requestParam.getString("permissionCode");
        Date startTime = requestParam.getDate("startTime");
        Date endTime = requestParam.getDate("endTime");
        ParametersCheckUtil.isNotBlank(permissionCode, "权限代码为空");
        ParametersCheckUtil.isNotBlank(organizationCode, "组织机构代码为空");
        permissionOuterService.clearWholePermissionCache(ConfigConstantValues.getLoginUserCacheDTO().getCode());
        return permissionBizService.bindPermissionToOrg(organizationCode, permissionCode, startTime, endTime, ConfigConstantValues.getLoginUserCacheDTO().getCode());
    }

    /**
     * 解除组织机构权限绑定
     *
     * @param requestParam
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "remove", method = RequestMethod.POST)
    public Result<Void> remove(@RequestBody JSONObject requestParam) {
        ParametersCheckUtil.mapIsNotNullOrderEmpty(requestParam, "没有传递任何参数");
        String organizationCode = requestParam.getString("organizationCode");
        String permissionCode = requestParam.getString("permissionCode");
        ParametersCheckUtil.isNotBlank(permissionCode, "权限代码为空");
        ParametersCheckUtil.isNotBlank(organizationCode, "组织机构代码为空");
        permissionOuterService.clearWholePermissionCache(ConfigConstantValues.getLoginUserCacheDTO().getCode());
        return permissionBizService.unbindPermissionFromOrg(organizationCode, permissionCode, ConfigConstantValues.getLoginUserCacheDTO().getCode());
    }
}
